var params = {
    "rows": 10, //设置每一页的条目数
    "page": 1, //设置当前的页码
    "maxpages": 5, //设置最多显示的页码数
    /** 接口自带参数 */
    "no": "",
    "name": "",
    "sex": "",
    "classid": request.classId
};

/**
 * 调用接口获取学生数据
 */
function init() {
    params.rows = parseInt($('#pagesize').val());
    params.no = $('#txtno').val();
    params.name = $('#txtname').val();
    jqPagingInitLogin('/classesstudentlist', params, 'divpage', 'settable');
}

/**
 * 绘制表格数据
 * @param obj
 * @returns {boolean}
 */
function settable(obj) {
    var noticeHtml = '';
    if (obj.table.length == 0) {
        noticeHtml += '<tr>';
        noticeHtml += '<td colspan="8">未检索到任何记录</td>';
        noticeHtml += '</tr>';
        $("#tbody").html(noticeHtml);
        return false;
    }
    for (var i = 0; i < obj.table.length; i++) {
        let student = obj.table[i];
        let id = student.id;
        let no = student.student_no;
        let sex = student.student_sex;
        let name = student.student_name;
        let birth = student.student_birth;
        let email = student.student_email;
        let desc = student.student_desc;
        noticeHtml += `<tr>
                        <td><input type="checkbox" class="checkbox" value="${id}"/></td>
                        <td name="no">${no}</td>
                        <td name="name">${name}</td>
                        <td name="sex">${sex == '1' ? '男' : '女'}</td>
                        <td name="birth">${birth}</td>
                        <td name="email">${email}</td>
                        <td name="desc">${desc}</td>
                    </tr>`;
    }
    $("#tbody").html(noticeHtml);
    $('input').iCheck({
        checkboxClass: 'icheckbox_square-red',
        radioClass: 'iradio_square-red',
        increaseArea: '20%' // optional
    });
}

/**
 * 弹出添加用户信息框
 */
function addUser() {
    layer.open({
        type: 2,
        title: '新增学生信息',
        closeBtn: 1, //显示关闭按钮
        shadeClose: false,
        area: ['550px', '550px'],
        content: 'studentselect.jsp'
    });
}

/**
 * 保存班级学生
 */
function setuser() {
    var tvalue = window.localStorage['uservalue'];
    console.log(tvalue);
    window.localStorage['uservalue'] = '';
    var newdata = {
        'studentId': tvalue,
        'classId': request.classId
    }
    if (tvalue != "") {
        var result = ApiPostLogin('/distributionstudent', JsonToStr(newdata));
        var resp = StrToJson(result);
        if (resp.error == "1") {
            layer.msg("操作成功", {icon: 1, time: 2000},function (){
                init();
                layer.closeAll();
            });
        } else {
            layer.msg("操作失败", {icon: 2, time: 2000},function (){
                layer.closeAll();
            });
        }
    }
}

/***
 * 删除学生信息
 */
function deleteUser() {
    if ($("input[type='checkbox'][id!='checkAll']:checked").length === 0) {
        layer.msg('请至少选择一个学生进行移出', {icon: 2, time: 2000});
        return false;
    }
    layer.confirm('确认要移出这些学生吗？', {
        btn: ['是', '否'] //按钮
    }, function () {
        let id = "";
        $("input[type='checkbox'][id!='checkAll']:checked").each(function () {
            id += $(this).val() + ",";
        });
        id = id.substr(0, id.length - 1);
        var data = {
            studentId: id,
            classId: request.classId,
        }
        var result = ApiPostLogin('/removeclassstudent', JsonToStr(data));
        if (result != -1) {
            console.log(result);
            var obj = eval('(' + result + ')');
            if (obj.error == "1") {
                layer.msg('删除成功', {icon: 1, time: 2000}, function () {
                    init();
                    layer.closeAll();
                });
            } else {
                layer.msg('失败');
            }
        }
        layer.closeAll();
    }, function () {
    });

}


$(function () {
    init();
    /**
     * 绑定checkbox和radio的样式
     */
    $('input').iCheck({
        checkboxClass: 'icheckbox_square-red',
        radioClass: 'iradio_square-red',
        increaseArea: '20%' // optional
    });
    /**
     * 绑定新增按钮事件
     */
    $("#btnAdd").click(function () {
        addUser();
    });
    /**
     * 绑定删除按钮事件方法
     */
    $("#btnDel").click(function () {
        deleteUser();
    });

    $("#btnBack").click(function () {
        parent.layer.closeAll();
    });
    /**
     * 绑定查询按钮事件方法
     */
    $("#btnQuery").click(function () {
        init();
    });
    /**
     * 绑定提交按钮事件方法
     */
    $("#btn_submit").click(function () {
        submit();
    });
    /**
     * 全选按钮，全选
     */
    $("#checkAll").on('ifChecked', function (event) {
        $("input[type='checkbox'][id!='checkAll']").iCheck('check');
    });
    /**
     * 全选取消
     */
    $("#checkAll").on('ifUnchecked', function (event) {
        $("input[type='checkbox'][id!='checkAll']").iCheck('uncheck');
    });

});